/**
 * @author: Loy 
 * @date: 2014-7-13-下午5:39:04
 */
package net.loyintean.pearlshell.validation.rule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.loyintean.pearlshell.validation.IValidRuleConfig;

/**
 * validatrion whether a string is in format of Email.
 * <p>
 * 校验一个字符串是否符合Email格式。
 * 
 * @author Loy
 * @version 2014-7-13.1.0.0
 */
class StringIsEmailValidRuleImpl extends AbstractStringValidRuleImpl {

	/**
	 * encapsulation.
	 * 
	 * @author Loy
	 */
	StringIsEmailValidRuleImpl() {
		super();
	}

	/**
	 * pattern for email. The pattern string is
	 * "/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/"
	 */
	private static final Pattern EMAIL = Pattern
			.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");

	/**
	 * " is not in format of Email{@code (xxx.yyy@zzz.com) }".
	 * <p>
	 * “不是电子邮箱格式{@code (xxx.yyy@zzz.com) }”。
	 * 
	 * @author Loy
	 * @see net.loyintean.pearlshell.validation.IValidRule
	 *      #errorMsg(net.loyintean.pearlshell.validation.IValidRuleConfig)
	 * 
	 */
	@Override
	public String errorMsg(IValidRuleConfig config) {
		return "不是电子邮箱格式(xxx.yyy@zzz.com)";
	}

	/**
	 * if the toValid is not in format of {@link #EMAIL}, then return false.<br>
	 * 如果tovalid不是email格式，则返回false。
	 * <p>
	 * the param config is not in use.<br>
	 * 另一个入参（config）没有使用。
	 * 
	 * @author Loy
	 * @see net.loyintean.pearlshell.validation.rule.AbstractStringValidRuleImpl
	 *      #validString(java.lang.String,
	 *      net.loyintean.pearlshell.validation.IValidRuleConfig)
	 * 
	 */
	@Override
	protected boolean validString(String toValid, IValidRuleConfig config) {
		assert toValid != null;

		Matcher matcher = EMAIL.matcher(toValid);

		return matcher.find();

	}

}
